namespace ResponseLocalLibrary
{
    using IAM.Interface.Model;
    using IAM.Interface.Model.ENUM;
    using System;
    using System.Data;

    public class DelegationMagagement
    {
        public AIDelegations GetDelegationInfo(AccountInfo accountinfo)
        {
            AccountInfo info = new AccountInfo();
            info.HRISEID = accountinfo.HRISEID;
            info.ADAccount = accountinfo.ADAccount;
            info.CSLAccount = accountinfo.CSLAccount;
            info.CILX500 = accountinfo.CILX500;
            info.UPI = accountinfo.UPI;
            info.AppAccount = accountinfo.AppAccount;
            Delegations delegations = new Delegations();
            DataTable delegationInfoByUser = CCSFacade.GetDelegationInfoByUser(accountinfo.UPI);
            if (delegationInfoByUser == null)
            {
                AIDelegations delegations2 = new AIDelegations();
                delegations2.AccountInfo = info;
                delegations2.Delegations = delegations;
                return delegations2;
            }
            try
            {
                foreach (DataRow row in delegationInfoByUser.Rows)
                {
                    Delegation delegation = new Delegation();
                    delegation.Module = (int.Parse(row["RoleID"].ToString()) + 10000).ToString();
                    string str = "both";
                    switch (str.ToLower())
                    {
                        case "delegate":
                            delegation.Type = ModuleType.Delegate;
                            goto Label_0126;

                        case "operateas":
                            delegation.Type = ModuleType.OperateAs;
                            break;

                        case "both":
                            delegation.Type = ModuleType.Both;
                            break;
                    }
                Label_0126:
                    delegation.BeginDate = Convert.ToDateTime(row["DelegateStartDate"]);
                    delegation.EndDate = Convert.ToDateTime(row["DelegateEndDate"]);
                    delegation.Delegator = new AccountInfo();
                    AccountInfo info2 = new AccountInfo();
                    info2.HRISEID = string.Empty;
                    info2.ADAccount = string.Empty;
                    info2.CSLAccount = string.Empty;
                    info2.CILX500 = string.Empty;
                    info2.UPI = row["DelegateUser"].ToString();
                    info2.AppAccount = row["DelegateUser"].ToString();
                    delegation.Delegator = info2;
                    delegations.Items.Add(delegation);
                }
                AIDelegations delegations3 = new AIDelegations();
                delegations3.AccountInfo = info;
                delegations3.Delegations = delegations;
                return delegations3;
            }
            catch
            {
                Delegations delegations4 = new Delegations();
                Delegation delegation2 = new Delegation();
                delegation2.Module = "Module1";
                delegation2.Type = ModuleType.OperateAs;
                delegation2.BeginDate = DateTime.Now;
                delegation2.EndDate = delegation2.BeginDate.AddMonths(3);
                delegation2.Delegator = new AccountInfo();
                delegation2.Delegator.HRISEID = "new1234567";
                delegation2.Delegator.ADAccount = @"newad4\lisi";
                delegation2.Delegator.CSLAccount = "newlisi";
                delegation2.Delegator.CILX500 = "newlisi";
                delegation2.Delegator.UPI = "CV-88889";
                delegation2.Delegator.AppAccount = "new CV-Li Si Account";
                Delegation delegation3 = new Delegation();
                delegation3.Module = "Module2...";
                delegation3.Type = ModuleType.OperateAs;
                delegation3.BeginDate = DateTime.Now;
                delegation3.EndDate = delegation3.BeginDate.AddMonths(3);
                delegation3.Delegator = new AccountInfo();
                delegation3.Delegator.HRISEID = "2new1234567";
                delegation3.Delegator.ADAccount = @"2newad4\lisi";
                delegation3.Delegator.CSLAccount = "2newlisi";
                delegation3.Delegator.CILX500 = "2newlisi";
                delegation3.Delegator.UPI = "2CV-88889";
                delegation3.Delegator.AppAccount = "2new CV-Li Si Account";
                delegations4.Items.Add(delegation2);
                delegations4.Items.Add(delegation3);
                AIDelegations delegations5 = new AIDelegations();
                delegations5.AccountInfo = info;
                delegations5.Delegations = delegations4;
                return delegations5;
            }
        }

        public Modules GetModuleList()
        {
            DataTable roleList = CCSFacade.GetRoleList();
            Modules modules = new Modules();
            try
            {
                for (int i = 0; i < roleList.Rows.Count; i++)
                {
                    Module module = new Module();
                    module.ID = (int.Parse(roleList.Rows[i]["RoleID"].ToString()) + 10000).ToString();
                    module.Display = roleList.Rows[i]["RoleName"].ToString();
                    string str = "both";
                    switch (str.ToLower())
                    {
                        case "delegate":
                            module.Type = ModuleType.Delegate;
                            goto Label_00C0;

                        case "operateas":
                            module.Type = ModuleType.OperateAs;
                            break;

                        case "both":
                            module.Type = ModuleType.Both;
                            break;
                    }
                Label_00C0:
                    modules.Items.Add(module);
                }
            }
            catch
            {
                Module module2 = new Module();
                module2.ID = "Module1";
                module2.Display = "Module 1";
                module2.Type = ModuleType.Delegate;
                Module module3 = new Module();
                module3.ID = "Module2..";
                module3.Display = "Module 2..";
                module3.Type = ModuleType.OperateAs;
                Module module4 = new Module();
                module4.ID = "Module3..";
                module4.Display = "Module 3..";
                module4.Type = ModuleType.Both;
                modules.Items.Add(module2);
                modules.Items.Add(module3);
                modules.Items.Add(module4);
            }
            return modules;
        }

        public AIDelegations SetDelegationInfo(AIDelegations aidelegations)
        {
            try
            {
                foreach (object obj2 in aidelegations.Delegations.Items)
                {
                    Delegation delegation = obj2 as Delegation;
                    if (delegation != null && String.IsNullOrEmpty(delegation.Delegator.UPI))
                    {
                        CCSFacade.DetachOneDelegationFromUser(aidelegations.AccountInfo.UPI, (int.Parse(delegation.Module) - 10000).ToString());
                        return this.GetDelegationInfo(aidelegations.AccountInfo);
                    }
                }
                CCSFacade.DetachAllDelegationFromUser(aidelegations.AccountInfo.UPI);
                foreach (object obj2 in aidelegations.Delegations.Items)
                {
                    Delegation delegation = obj2 as Delegation;
                    if (delegation != null && !String.IsNullOrEmpty(delegation.Delegator.UPI))
                    {
                        CCSFacade.SetDelegationInfo(aidelegations.AccountInfo.UPI, delegation.Delegator.UPI, (int.Parse(delegation.Module) - 10000).ToString(), delegation.BeginDate.ToString("M/d/yyyy"), delegation.EndDate.ToString("M/d/yyyy"));
                    }
                }
                AIDelegations delegations = new AIDelegations();
                return this.GetDelegationInfo(aidelegations.AccountInfo);
            }
            catch
            {
                return null;
                //Delegations delegations2 = new Delegations();
                //Delegation delegation2 = new Delegation();
                //delegation2.Module = "Module1";
                //delegation2.Type = ModuleType.OperateAs;
                //delegation2.BeginDate = DateTime.Now;
                //delegation2.EndDate = delegation2.BeginDate.AddMonths(3);
                //delegation2.Delegator = new AccountInfo();
                //delegation2.Delegator.HRISEID = "new1234567";
                //delegation2.Delegator.ADAccount = @"newad4\lisi";
                //delegation2.Delegator.CSLAccount = "newlisi";
                //delegation2.Delegator.CILX500 = "newlisi";
                //delegation2.Delegator.UPI = "CV-88889";
                //delegation2.Delegator.AppAccount = "new CV-Li Si Account";
                //Delegation delegation3 = new Delegation();
                //delegation3.Module = "Module2...";
                //delegation3.Type = ModuleType.OperateAs;
                //delegation3.BeginDate = DateTime.Now;
                //delegation3.EndDate = delegation3.BeginDate.AddMonths(3);
                //delegation3.Delegator = new AccountInfo();
                //delegation3.Delegator.HRISEID = "2new1234567";
                //delegation3.Delegator.ADAccount = @"2newad4\lisi";
                //delegation3.Delegator.CSLAccount = "2newlisi";
                //delegation3.Delegator.CILX500 = "2newlisi";
                //delegation3.Delegator.UPI = "2CV-88889";
                //delegation3.Delegator.AppAccount = "2new CV-Li Si Account";
                //delegations2.Items.Add(delegation2);
                //delegations2.Items.Add(delegation3);
                //AIDelegations delegations3 = new AIDelegations();
                //delegations3.AccountInfo = aidelegations.AccountInfo;
                //delegations3.Delegations = delegations2;
                //return delegations3;
            }
        }
    }
}

